home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / QuickDraw™ GX / Programming Stuff / Sample Code / Graphics Samples / Bitmap Shape with Clip ƒ / Bitmap Shape with Clip.c next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  7.5 KB  |  225 lines  |  [TEXT/KAHL]

  1. /**
  2.  --
  3.  --        App:        Bitmap Shape with Clip
  4.  --
  5.  -- 
  6.  --        File:        Bitmap Shape with Clip.c
  7.  --
  8.  --
  9.  --        Comments:    This code the ability of QuickDraw GX to clip any shape with any geometric shape. 
  10.  --                    In this case, we retrieve a bitmap shape from the resource fork of the application, 
  11.  --                    and clip it with a text shape - "BEACH". We collect both shapes into a GX picture, 
  12.  --                    thereby allowing us to make one call to draw both shapes.retrieves a bitmap shape
  13.  --                    from the resource fork of the app. It creates the text shape 
  14.  --
  15.  --
  16.  --        Version:    1.0     4/94:    added the capability to dynamically determine the amount of
  17.  --                                    scaling required to make the clip shape clip the entire bitmap
  18.  --                                    shape.    
  19.  --
  20.  --                             3/93:    created     
  21.  --
  22.  --
  23.  --        Components:    Bitmap Shape with Clip.c
  24.  --                    graphics shell.c
  25.  --                    graphics shell.h
  26.  --
  27.  --
  28.  --        QuickDraw GX
  29.  --        Libraries
  30.  --        Used:        This application uses the following QuickDraw GX library code files:
  31.  --                    "color library.c", "font library.c", "graphics debug library.c",
  32.  --                    "qd library.c", and "transform library.c". 
  33.  --        
  34.  --        
  35.  --        Notes:        1) Print this file in landscape for the best results
  36.  --                    2) If you are using THINK C v5.x, I have added THINK markers to navigate the code.
  37.  --                    3) This code was adapted from the "One Rectangle" QuickDraw GX sample.
  38.  --
  39.  --
  40.  --        Author:        Pete "Luke" Alexander
  41.  --                    Developer Technical Support
  42.  --                    AppleLink: DEVSUPPORT
  43.  --
  44.  --        
  45.  --        ©1992 - 1994  Apple Computer, Inc. 
  46.  --        All rights reserved.
  47.  --
  48.  **/
  49.  
  50. #include <events.h>
  51. #include <windows.h>
  52.  
  53. #include "Font library.h" 
  54. #include "graphics debugging.h"
  55. #include "graphics libraries.h"
  56. #include "graphics toolbox.h"
  57. #include "qd library.h"
  58. #include "graphics shell.h"
  59.  
  60.  
  61. #define kCheckBitmapShape
  62.  
  63. //
  64. //  Set up the title and size of the window 
  65. //
  66. Str255         gWindowTitle = "\p Bitmap Shape with Clip ";
  67. Rect         gWindowQDRect  = {50, 20, 345, 455};
  68.  
  69. //
  70. //    gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
  71. //    in main () within graphics shell.c.  You can determine the amount of graphics gxHeap required by using GraphicsBug.
  72. //    With  gGraphicsHeapSize set to 48k,I had 6 free blocks left in the graphics gxHeap and
  73. //    I was not receiving any memory related warnings or notices from GX....
  74. //
  75. long        gGraphicsHeapSize = 48;
  76.  
  77. gxShape     gthePicture;
  78.  
  79.  
  80.  
  81. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  82.  
  83. void DoInitialization(gWindow)
  84. WindowPtr gWindow;
  85. {
  86.     gxShape        newClipShape, clipShapeOutline;
  87.     gxShape        tempBitmapShape;
  88.     float        xScaleFactor, yScaleFactor;
  89.     fixed         clipShapeWidth,clipShapeHeight;
  90.     fixed         bitmapWidth, bitmapHeight;
  91.     gxRectangle    bitmapBounds, clipShapeBounds;
  92.  
  93.  
  94.     InitCommonColors ();
  95.     
  96.     gthePicture = GXNewShape(gxPictureType);
  97.  
  98.     //
  99.     //    Retrieve the bitmap shape form the resource fork of the application. With the "debugging" init installed, we
  100.     //    can check to see if the shape was retrieved correctly by calling the shape validation routine. If the shape is
  101.     //    not valid, we will recieve a warning in the debugger.
  102.     //
  103.     tempBitmapShape = GetPixMapShape(129);
  104.     
  105.     #ifdef kCheckBitmapShape
  106.         GXValidateShape (tempBitmapShape);
  107.     #endif
  108.  
  109.     //
  110.     //    Define the text shape to clip our bitmap shape with. In this case, our clip shape will be a text
  111.     //    shape which uses a text size of 96 point and the Helvetica font.
  112.     //
  113.     newClipShape = GXNewText( 5,(unsigned char*)"BEACH", nil );
  114.     SetShapeCommonFont( newClipShape, helveticaFont );
  115.     GXSetShapeTextSize( newClipShape, ff(96) );
  116.  
  117.     //
  118.     //    Determine the bounds of our bitmap and clip shape and move the clip shape to the top left corner 
  119.     //    of the bitmap shape. 
  120.     //
  121.     GXGetShapeBounds( tempBitmapShape, 0, &bitmapBounds );
  122.     GXGetShapeBounds( newClipShape, 0, &clipShapeBounds );
  123.  
  124.     clipShapeHeight = clipShapeBounds.bottom - clipShapeBounds.top;
  125.     
  126.     GXMoveShapeTo ( newClipShape, bitmapBounds.left - ff(7), clipShapeHeight + bitmapBounds.top  - ff(2) );
  127.  
  128.     //
  129.     //    We turn off the metrics and contour capabilites of TrueType to enable GX to
  130.     //    scale the clip shape linearly. We convert our text shape to a path shape because
  131.     //    you cannot use a text shape as a clip shape. A clip shape can only be a geometric shape
  132.     //    (i.e. a point, line, rectangle, polygon, curve, or path).
  133.     //
  134.     GXSetShapeTextAttributes ( newClipShape, gxNoMetricsGridText | gxNoContourGridText );
  135.     GXPrimitiveShape ( newClipShape );
  136.     
  137.     //
  138.     //    Since, we just moved the clip shape, we need to determine the bounds of our shape again.
  139.     //    We can now determine the amount we need to scale our clip shape to cover our bitmap
  140.     //    shape. This is accomplished by determining the differences between the height and width
  141.     //    of the bitmap and clip shapes. We will then use these differences as the scale factors
  142.     //    when we call GXScaleShape (..) to scale up our clip shape.
  143.     //
  144.     //    The "fl" macro converts a floating point number into a fixed point number.
  145.     //
  146.     GXGetShapeBounds( newClipShape, 0, &clipShapeBounds );
  147.  
  148.     clipShapeWidth = clipShapeBounds.right - clipShapeBounds.left;
  149.     bitmapWidth = bitmapBounds.right - bitmapBounds.left;
  150.     xScaleFactor = FixedToFloat( bitmapWidth ) / FixedToFloat ( clipShapeWidth );
  151.     
  152.     clipShapeHeight = clipShapeBounds.bottom - clipShapeBounds.top;
  153.     bitmapHeight = bitmapBounds.bottom - bitmapBounds.top;
  154.     yScaleFactor = FixedToFloat( bitmapHeight ) / FixedToFloat ( clipShapeHeight );
  155.  
  156.     GXScaleShape( newClipShape, fl(xScaleFactor),  fl(yScaleFactor), bitmapBounds.left, bitmapBounds.top );
  157.  
  158.     //
  159.     // Set the clip of our bitmap shape to our text shape and add it to our picture.
  160.     //
  161.     GXSetShapeClip( tempBitmapShape, newClipShape );
  162.     GXSetPictureParts(gthePicture, 0, 0, 1, &tempBitmapShape, nil, nil, nil );
  163.     GXDisposeShape ( tempBitmapShape );
  164.  
  165.     //
  166.     //    Change the fill of our text shape be to the outline of the text, set the size used, set the pen to cruise on the outside
  167.     //    of the contour of each letter, and add it to our picture shape.
  168.     //
  169.     GXSetShapeFill( newClipShape, gxClosedFrameFill );
  170.     GXSetShapeStyleAttributes( newClipShape, gxOutsideFrameStyle );
  171.     GXSetShapePen( newClipShape, ff(3));
  172.     SetShapeCommonColor( newClipShape, blue);
  173.  
  174.     GXSetPictureParts(gthePicture, 0, 0, 1, &newClipShape, nil, nil, nil);
  175.     GXDisposeShape ( newClipShape );
  176.  
  177.     GXMoveTransform(GXGetShapeTransform( gthePicture ), ff(20), ff(15) );    
  178. }
  179.  
  180.  
  181. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  182.  
  183. void DoDraw(gWindow)
  184. WindowPtr gWindow;
  185. {
  186.     GXDrawShape (gthePicture);
  187. }
  188.  
  189.  
  190. /*------ DoDispose -------------------------------------------------------------------------------------*/
  191.  
  192. void DoDispose(gWindow)
  193. WindowPtr gWindow;
  194. {
  195.     /**  
  196.         You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  197.         form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  198.         call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  199.         SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  200.         can turn notices on in this file by setting gDebugging = TRUE (above).
  201.     **/
  202.     DisposeCommonColors ();
  203.     GXDisposeShape(gthePicture);  
  204.      GXDisposeShape(gWindowBoundsShape);  
  205.     DisposeWindow(gWindow);
  206. }
  207.     
  208.  
  209.  
  210. /*------ DoClick ---------------------------------------------------------------------------------------*/
  211.  
  212. void DoClick( orgMouseLoc, theWindow )
  213. gxPoint        orgMouseLoc;
  214. WindowPtr     theWindow;
  215. {
  216. }
  217.  
  218.  
  219. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  220.  
  221. void DoIdle(gWindow)
  222. WindowPtr gWindow;
  223. {
  224. }
  225.